using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Threading;
using DP.Sharepoint.Workflow.Tests.ForTesting;
using Microsoft.SharePoint.Workflow;

namespace DP.Sharepoint.Workflow.Tests.ForTesting
{
    public class StubSharePointService : ISharePointService
    {
        #region ISharePointService Members

        public event EventHandler<SPActivationEventArgs> OnWorkflowActivated;
        public event EventHandler<SPItemEventArgs> OnWorkflowItemChanged;
        public event EventHandler<SPItemEventArgs> OnWorkflowItemDeleted;

        public void LogToHistoryList(Guid workflowId, SPWorkflowHistoryEventType eventId, int userId, TimeSpan duration,
                                     string outcome, string description, string otherData)
        {
            Trace.WriteLine(string.Format(
                                "LogToHistoryList | WorkflowID {0} | EventType {1}| userid {2} | outcome {3}",
                                workflowId,
                                eventId, userId, outcome));
        }

        public void SetState(Guid workflowId, int index, int state)
        {
            Trace.WriteLine(string.Format("SetState - WorkflowID {0} | index {1}| state {2}", workflowId, index, state));
        }

        public void SendEmail(Guid workflowId, bool includeStatus, StringDictionary headers, string body)
        {
            Trace.WriteLine(string.Format("SendEmail | WorkflowID {0}", workflowId));
        }

        public void UpdateAllTasks(Guid workflowId, SPWorkflowTaskProperties properties)
        {
            Trace.WriteLine(string.Format("UpdateAllTasks | WorkflowID {0}", workflowId));
        }

        public void InitializeWorkflow(Guid workflowId)
        {
            Trace.WriteLine(string.Format("InitializeWorkflow| WorkflowID {0}", workflowId));
        }

        #endregion


        public void PostEventOnWorkflow(WorkflowRuntimeManager.MyEvent eventData)
        {
            ThreadPool.QueueUserWorkItem(WorkflowEvent, eventData);
        }
        //public void TriggerWorkflowActivated(Guid workflowId, SPActivationEventArgs arguments)
        //{
        //    ThreadPool.QueueUserWorkItem(WorkflowActivatedEvent, arguments);
        //}

        private void WorkflowEvent(object stateInfo)
        {
            Trace.WriteLine("\nService:  Loading workflow due to event firing...");
            WorkflowRuntimeManager.MyEvent eventData = (WorkflowRuntimeManager.MyEvent) stateInfo;

            switch(eventData.EventName)
            {
                case "OnWorkflowActivated":
                    EventHandler<SPActivationEventArgs> activatedEvent = OnWorkflowActivated;
                    if (activatedEvent != null)
                        activatedEvent(null, (SPActivationEventArgs)eventData.Arguments);

                    break;
                default:
                    break;
            }
        }

        //public void WorkflowActivatedEvent(object stateInfo)
        //{
        //       Trace.WriteLine("\nService:  Loading workflow due to event firing...");
        //       SPActivationEventArgs arguments = (SPActivationEventArgs) stateInfo;
        //}
    }
}